package com.tong.hao.ac.api.validation.validator;

import cn.buddha.jec.util.TongSpringContextUtils;
import com.tong.hao.ac.api.innerapi.api.IAcService;
import com.tong.hao.ac.api.validation.constraints.AcBillCategoryCheck;
import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Map;

/**
 * @ClassName AcBillCategoryValidator
 * @Author 友野浩二
 * @Date 2020/4/26 16:39
 * @Description 账单类型检验
 * @Version 1.0
 *
 * <pre>
 * 自定义校验逻辑处理类:
 *      必须实现 javax.validation.ConstraintValidator 接口，该接口需要两个泛型参数
 *          1).定义的参数校验的注解;
 *          2).该注解用于什么类型的数据
 * </pre>
 */
@Slf4j
public class AcBillCategoryValidator implements ConstraintValidator<AcBillCategoryCheck, Object> {

//    @Autowired    // 这种方式行不通，暂时不知道为啥
//    private IAcService acService;

    @Override
    public void initialize(AcBillCategoryCheck constraintAnnotation) {
        log.info("initialize com.tong.hao.ac.api.validation.validator.AcBillCategoryValidator");
    }

    /**
     * @return boolean
     * @Author 友野浩二
     * @Description // 返回true,校验成功；返回false,校验失败!
     * @Date 2020/5/5 0:55
     * @Param [value, context]
     */
    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        log.info("需要校验的账单类型编号: {}", value);
        // 获取接口 IAcService 的实现类
        Map<String, IAcService> iAcServiceMap = TongSpringContextUtils.getBeansOfType(IAcService.class);
        IAcService acService = iAcServiceMap.get("acServiceImpl");
        // 账单类型校验
        return acService.isContains(value, IAcService.IS_CONTAINS_BILL_CATEGORY);
    }
}
